home *** CD-ROM | disk | FTP | other *** search
-
- /*******************************************************************************
- ********************************************************************************
- ********************************************************************************
-
- PERMISSION TO COPY THIS SOFTWARE IS HEREBY GIVEN BY THE AUTHOR PROVIDED THAT
- THIS LEADING MESSAGE IS INCLUDED IN ALL OF THE RELEVANT SOURCE FILES.
-
- P. SCHMITZ, UNIVERSITY OF KEELE, MAY 1988.
-
-
- ********************************************************************************
- ********************************************************************************
- *******************************************************************************/
-
- #include "files.h"
- #include "ships.h"
- #include "header.h"
- FILE *f, *fopen();
-
- putmasterpidintofile()
- {
- int masterpid;
- char string[15];
-
- masterpid=getpid();
- sprintf(string,"%d\n",masterpid);
- i=creat(MASTERPIDFILE,0666);
- if ((f=fopen(MASTERPIDFILE,"w"))==NULL)
- {
- die();
- } else
- {
- fputs(string,f); /*write master pid into file*/
- fclose(f);
- }
- }
-
- find_player_id()
- {
- int i;
-
- if (noofplayers>=10)
- {
- return(0);
- }
- i=0;
- while ((i<10)&&(players[i]==1)) ++i;
- players[i]=1;
- return(i);
- }
-
- makenewplayerandjoinhimin()
- {
- char string[80];
- char *malloc();
- struct player *aa,*xp;
-
- if (noofplayers==10)
- {
- unlink(LOCK);
- return(0);
- } else
- {
- if ((x=open(PLAYERFILE,0))<0)
- {
- unlink(LOCK);
- return(0);
- }
- else
- {
- /* find a free player position */
- aa=(struct player *)malloc(sizeof(struct player));
-
- /*NOW READ IN THE DATA FROM PLAYERFILE*/
- i=0;
- read(x,&c,1);
- while (c!='#')
- {
- if (i>13) {fprintf(stderr,"1:i went out:%d",i); die();}
- aa->terminalfile[i]=c;
- i++;
- read(x,&c,1);
- }
- i=0;
- read(x,&c,1);
- while (c!='#')
- {
- if (i>19) {fprintf(stderr,"2:i went out:%d",i); die();}
- aa->cm[i]=c;
- i++;
- read(x,&c,1);
- }
- i=0;
- read(x,&c,1);
- while (c!='#')
- {
- if (i>19) {fprintf(stderr,"3:i went out:%d",i); die();}
- aa->cl[i]=c;
- i++;
- read(x,&c,1);
- }
- i=0;
- read(x,&c,1);
- while (c!='#')
- {
- if (i>79) {fprintf(stderr,"4:i went out:%d",i); die();}
- string[i]=c;
- i++;
- read(x,&c,1);
- }
- for(y=i;y<80;y++) string[i]='\0';
- aa->shipno=atoi(string);
-
- i=0;
- read(x,&c,1);
- while (c!='#')
- {
- if (i>79) {fprintf(stderr,"5:i went out:%d",i); die();}
- string[i]=c;
- i++;
- read(x,&c,1);
- }
- for(y=i;y<80;y++) string[i]='\0';
- aa->pid=atoi(string);
-
- i=0;
- read(x,&c,1);
- while (c!='#')
- {
- if (i>79) {fprintf(stderr,"5:i went out:%d",i); die();}
- string[i]=c;
- i++;
- read(x,&c,1);
- }
- for(y=i;y<80;y++) string[i]='\0';
- aa->tty_type=atoi(string);
-
- close(x);
- /*NOW READ IN THE DATA FROM PLAYERFILE*/
-
- aa->terminalfile[13]=' ';
- /*
- fprintf(stderr,"shipno:%d\npid:%d\ntty_type:%d\n",aa->shipno,aa->pid,aa->tty_type);
- fprintf(stderr,"cm is :");
- for (i=0;i<20;i++) fprintf(stderr,"*%c*",aa->cm[i]);
- fprintf(stderr,"\ncl is :");
- for (i=0;i<20;i++) fprintf(stderr,"*%c*",aa->cl[i]);
- fprintf(stderr,"\nThats it\n");
- read(0,&c,1);
- */
- if (((aa->fd)=open((aa->terminalfile),2))==-1)
- {
- kill(aa->pid,9);
- free(aa);
- killcontrol();
- } else
- {
- ++noofplayers;
- aa->doneit=0;
- aa->impulsemove=0;
- aa->reason=10;
- aa->id=(char)('a'+find_player_id());
- aa->xpos=230;
- aa->ypos=(int)(rnd(0,5)+675);
- aa->oldxpos=aa->xpos;
- aa->oldypos=aa->ypos;
- aa->beeping=ON;
- aa->maxvel=MAXWARP;
- aa->xvel=0;
- aa->yvel=0;
- aa->chflg=FLAG;
- aa->clflg=OFF;
- aa->stat=3;
- aa->energy=STARTENERGY;
- aa->maxenergy=STARTENERGY;
- aa->phaserbanks=STARTPHASER;
- aa->maxphaser=STARTPHASER;
- aa->photontorpedos=STARTPHOTON;
- aa->maxphoton=STARTPHOTON;
- aa->photonpower=PHOTONPOWER;
- aa->phaserpower=PHASERPOWER;
- aa->photonreach=WEAPONREACH;
- aa->phaserreach=WEAPONREACH;
- aa->weaponselect=0;
- aa->score=0;
- aa->cloakingdevice=OFF;
- aa->shields=OFF;
- for(k=0;k<30;++k)
- aa->planets[k]=0;
- aa->planetno=0;
- for (k=0; k<=3; k++)
- {
- aa->channel[k].enemy=NULL;
- aa->channel[k].xloc=0;
- aa->channel[k].yloc=0;
- }
- aa->flyto= -1;
- broadcast("They advise us that new players are coming into the game.");
- flushbuf(aa);
- addtolist(aa);
- drawcross(aa);
- plotdata(aa);
- drawconsole(aa);
- plot1(aa,"Permission to come aboard granted. Captain on the Bridge.");
- plot2(aa," Welcome to the ");
- poscurs(aa,49,22);
- write(aa->fd,ship[aa->shipno].name,20);
-
- } /*else*/
- } /*else*/
- } /*else*/
- unlink(PLAYERFILE);
- unlink(LOCK);
-
- /*BLOW UP ALL ALIENS*/
-
- if (noofplayers==2)
- {
- xp=startlist;
- while (xp!=NULL)
- {
- if ((xp->id==ROMID)||(xp->id==KLINGID))
- finish(xp);
- xp=xp->next;
- }
- }
- }
-
- addtolist(x)
- struct player *x;
- {
- if ((startlist==NULL) && (taillist==NULL))
- {
- startlist=x; taillist=x; (startlist->next)=NULL;
- } else
- {
- x->next=NULL;
- taillist->next=x;
- taillist=x;
- }
- }
-
- remove_player_from_list(q)
- register struct player *q;
- {
- FILE *f,*fopen();
- struct player *a,*b;
- struct torpedo *t;
- char string[50];
- int listlength,l,alien;
-
- alien=0;
- if ((q->id==ROMID)||(q->id==KLINGID)) alien=1;
-
- if (alien) goto skipfile;
-
- while ((f=fopen(PLAYERSCOREFILE,"r"))!=NULL)
- {
- fclose(f);
- }
- i=creat(PLAYERSCOREFILE,0666);
- if ((f=fopen(PLAYERSCOREFILE,"w"))==NULL)
- fprintf(stderr,"newplayer.c : Cant open PLAYERSCOREFILE.\n");
- sprintf(string,"%d %d\n",q->reason,q->score);
- fputs(string,f);
- fclose(f);
-
- kill(q->pid,9); /*this kills the users dummy process*/
- /*make his playerid available again.*/
- if ((q->id>='A')&&(q->id<='Z')) q->id=(char)((q->id)+32);
- l=(int)((q->id)-'a');
- players[l]=0;
- close(q->fd);
- /*make his playerid available again.*/
-
- skipfile :
-
- listlength=noofplayers+noofaliens;
-
- if (listlength>1)
- {
- t=tstartlist;
- while (t!=NULL)
- {
- if (t->whofrom==q) t->whofrom=NULL;
- t=t->next;
- }
-
- b=startlist;
- while (b!=NULL)
- {
- for (i=0; i<5; i++)
- if (b->channel[i].enemy==q) b->channel[i].enemy=NULL;
- b=b->next;
- }
-
- if (q==startlist)
- {
- a=startlist;
- startlist=a->next;
- q=NULL;
- free(a);
- } else
- {
- b=startlist;
- while (b->next!=q) b=b->next;
- b->next=q->next;
- if (q==taillist) taillist=b;
- free(q);
- q=NULL;
- }
-
- } else
- {
- startlist=NULL;
- taillist=NULL;
- a=NULL;
- b=NULL;
- free(q);
- q=NULL;
- }
- if (alien) --noofaliens; else --noofplayers;
- if (noofplayers==0)
- {
- unlink(MASTERPIDFILE);
- die();
- }
- killcontrol();
- }
-
-